package com.linxi.handy.component.listener;

import com.linxi.handy.common.Constants;
import com.linxi.handy.component.list.MouseProcessListener;
import com.linxi.handy.util.ConnectionUtils;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 面板
 *
 * @author user
 */
public class DatabaseList extends JPanel {

    public static DefaultMutableTreeNode top = new DefaultMutableTreeNode("mysql-root");
    static DefaultTreeModel dtm = new DefaultTreeModel(top);
    public static JTree tree = new JTree(dtm);
    static JButton refresh = new JButton("刷 新");
    /**
     * 构造界面所用的组件
     * 树状列表
     */
    JScrollPane treesPane = new JScrollPane();
    JPanel buttonPanel = new JPanel();
    JLabel delete = new JLabel("<HTML><U>删除...</U></HTML>");

    public DatabaseList() {
        addTree();
        tree.addMouseListener(new TreeListener());
        treesPane.setViewportView(tree);

        refresh.addActionListener(new ButtonListener());
        delete.addMouseListener(new MouseProcessListener());
        buttonPanel.add(refresh);
        buttonPanel.add(delete);

        this.setLayout(new BorderLayout());
        this.add(treesPane);
        this.add(buttonPanel, BorderLayout.SOUTH);
        /**
         * 设置控件属性 设置显示字体
         */
        tree.setFont(Constants.DEFAULT_FONT_13);
        refresh.setBackground(Color.WHITE);
    }

    public static void addTree() {
        tree.clearSelection();
        top.removeAllChildren();
        try {
            Statement stmt = ConnectionUtils.conn.createStatement();
            ResultSet rs = stmt.executeQuery("select schema_name from information_schema.schemata");
            while (rs.next()) {
                String name = rs.getString("schema_name");
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
                addNode(node, name);
                top.add(node);
            }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        tree.setRootVisible(true);
        tree.expandRow(0);
        tree.setRootVisible(false);
        tree.updateUI();

        //定义绘制对象变量
        DefaultTreeCellRenderer treeCellRenderer;
        //获得tree树的绘制对象
        treeCellRenderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
        //设置叶子节点不采用图标
        treeCellRenderer.setLeafIcon(null);
        treeCellRenderer.setClosedIcon(null);
        //设置展开时不采用图标
        treeCellRenderer.setOpenIcon(null);
    }

    static void addNode(DefaultMutableTreeNode node, String name) {
        try {
            Statement stmt = ConnectionUtils.conn.createStatement();
            ResultSet rs = stmt.executeQuery("select table_name from information_schema.tables where table_schema ='" + name + "'");
            while (rs.next()) {
                DefaultMutableTreeNode childrenNode = new DefaultMutableTreeNode(rs.getString("table_name"));
                node.add(childrenNode);
            }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

